﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;

namespace PeisongWeb
{
    public partial class firstpwdEdit : System.Web.UI.Page
    {
        protected int customerId
        {
            get
            {
                int tmp = 0;
                string tmpCustomerId = string.Empty;

                try
                {
                    tmpCustomerId = Library.StringLbl.EncryptRc2Decrypt(FromBase64(Request.QueryString["customid"]));
                }
                catch
                {
                    tmpCustomerId = string.Empty;
                }

                if (tmpCustomerId!=string.Empty)
                {
                    int.TryParse(tmpCustomerId,out tmp);
                }

                return tmp;
            }
            set
            { customerId = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }

        }
        protected void lbtn_Submit_Click(object sender, EventArgs e)
        {
            if (Request.Headers["Accept"] != "*/*")
            {
                if (customerId != 0)
                {
                    string oldpwd = Library.StringLbl.EncryptMD5(this.txt_oldPassword.Text);
                    int userid = customerId;
                    string sqlpwd = logic.customer.getById(userid).Password;
                    if (oldpwd == sqlpwd)
                    {
                        CustomerEntity _cus = new CustomerEntity();
                        _cus.CustomerId = userid;
                        _cus.Password = Library.StringLbl.EncryptMD5(txt_password.Text);
                        _cus.Oldpassword = oldpwd;
                        _cus.IP = GetClientIP();
                        logic.customer.updatePwd(_cus);
                        Library.Script.ClientMsg("修改成功！");
                        Response.Redirect("/index.html");
                    }
                    else
                    {
                        //旧密码不正确
                        Library.Script.ClientMsg("旧密码不正确！");
                        return;
                    }
                }
                else
                {
                    Library.Script.ClientMsg("非法参数！");
                    return;
                }
            }
        }
        public static string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == string.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == string.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }

        private string FromBase64(string result)
        {
            byte[] bytes_2 = Convert.FromBase64String(result);
            return System.Text.Encoding.Default.GetString(bytes_2);
        }
    }
}
